package model

import (
	"encoding/json"
	"errors"

	"gddgame.cc/galaxy/common/dsl/game/achievement"
	"gddgame.cc/galaxy/common/dsl/game/ad"
	"gddgame.cc/galaxy/common/dsl/game/config"
	"gddgame.cc/galaxy/common/dsl/game/reward"
	"gddgame.cc/galaxy/common/dsl/game/user"
	"gddgame.cc/galaxy/project/maze/maze/model/player_single"
	"gddgame.cc/galaxy/utils"
)

//go:generate msgp

const (
	// 竞速类
	SpeedGame user.GameType = iota
	FindGame
)

//msgp:tuple PlayerResult
type PlayerResult struct {
	Entity string `json:"entity" msg:"entity"`
	Score  int    `json:"score" msg:"score"`
	Name   string `json:"name" msg:"name"`
	Find   int    `json:"find" msg:"find"`
}

//msgp:tuple GameResult
type GameResult struct {
	// 获取的奖励
	Reward  *reward.GoldReward   `json:"reward" msg:"reward"`
	Success bool                 `json:"success" msg:"success"`
	Self    *PlayerResult        `json:"self" msg:"self"`
	Other   *PlayerResult        `json:"other" msg:"other"`
	Score   int                  `json:"score" msg:"score"`
	Result  *user.UserMessageExt `json:"result" msg:"result"`
}

//msgp:tuple GameUserExtend
type GameUserExtend struct {
	SingleStar  int `json:"singleStar" msg:"singleStar"`
	SingleScore int `json:"singleScore" msg:"singleScore"`
	SingleRank  int `json:"singleRank" msg:"singleRank"`
	OnlineScore int `json:"onlineScore" msg:"onlineScore"`
	OnlineRank  int `json:"onlineRank" msg:"onlineRank"`

	Gold int `json:"gold" msg:"gold"`

	Entity string `json:"entity" msg:"entity"`

	Achievements []*achievement.UserAchievement `json:"achievements" msg:"achievements"`
}

func (user *GameUserExtend) BindGameUser(gameUser *user.GameUser) {
	gameUser.Extend = user
}

//msgp:tuple GameOtherUserExtend
type GameOtherUserExtend struct {
	SingleStar  int `json:"singleStar" msg:"singleStar"`
	SingleScore int `json:"singleScore" msg:"singleScore"`
	OnlineScore int `json:"onlineScore" msg:"onlineScore"`

	Entity string `json:"entity" msg:"entity"`
}

func (user *GameOtherUserExtend) BindGameUser(gameUser *user.GameOtherUser) {
	gameUser.Extend = user
}
func (user *GameOtherUserExtend) BindRankUser(rankUser *user.RankUser) {
	rankUser.Extend = user
}

//msgp:tuple GamePlayerExtend
type GamePlayerExtend struct {
	NickName string `json:"nick_name" msg:"nick_name"`
	Avatar   string `json:"avatar" msg:"avatar"`
}

//msgp:tuple GameSingleResult
type GameSingleResult struct {
	Before  *player_single.SingleResult `json:"before" msg:"before"`
	Current *player_single.SingleResult `json:"current" msg:"current"`
	Result  *user.UserMessageExt        `json:"result" msg:"result"`
}

const (
	_ user.MessageType = iota
	ShareMessage
	AchievementMessage
	SingleMessage
	OnlineMessage
)

const (
	BaseAdPlane ad.AdPlane = iota
	SingleAdPlane
)

const (
	AdStat        = "ad"
	PlaybookTimes = "playbookTimes"
	PlaybookTime  = "playbookTime"
	MaxLogin      = "maxlogin"
	CurrentLogin  = "currentlogin"
	GoldStat      = "gold"
)

var (
	MessageTemplates = map[user.MessageType]map[string]string{
		ShareMessage: {
			"zh": "{nick_name}加入，领取分享奖励",
			"en": "",
		},
		AchievementMessage: {},
	}
)

var (
	DefaultBaseId      = 0
	DefaultEntitys     = []string{"Dwarf"}
	DefaultShareReward = reward.Reward{
		Gold: 10,
	}
	GoldAchievement   = 0
	AdAchievement     = 0
	SingleLeaderboard = ""
	OnlineLeaderboard = ""
	FindGameNumber    = 0

	AdBrandList = []string{"unity"}

	OnlineReward = &reward.GoldReward{Gold: 10}
)

func InitConfig(config *config.SettingConfig) (interface{}, error) {
	var err error
	switch config.Key {
	case "DefaultBaseId":
		DefaultBaseId = utils.ToInt(config.Value)
		return DefaultBaseId, nil
	case "DefaultEntitys":
		err = json.Unmarshal(utils.ToBytes(config.Value), &DefaultEntitys)
		return DefaultEntitys, err
	case "MessageTemplates":
		err = json.Unmarshal(utils.ToBytes(config.Value), &MessageTemplates)
		return MessageTemplates, err
	case "DefaultShareReward":
		err = json.Unmarshal(utils.ToBytes(config.Value), &DefaultShareReward)
		return DefaultShareReward, err
	case "GoldAchievement":
		GoldAchievement = utils.ToInt(config.Value)
		return GoldAchievement, nil
	case "AdAchievement":
		AdAchievement = utils.ToInt(config.Value)
		return AdAchievement, nil
	case "AdBrandList":
		err = json.Unmarshal(utils.ToBytes(config.Value), &AdBrandList)
		return AdBrandList, err
	case "OnlineLeaderboard":
		OnlineLeaderboard = utils.ToString(config.Value)
		return OnlineLeaderboard, nil
	case player_single.SingleLeaderboard:
		SingleLeaderboard = utils.ToString(config.Value)
		return SingleLeaderboard, nil
	case "OnlineReward":
		err = json.Unmarshal(utils.ToBytes(config.Value), OnlineReward)
		return OnlineReward, err
	case "FindGameNumber":
		FindGameNumber = utils.ToInt(config.Value)
		return FindGame, nil
	}
	return nil, errors.New("config " + config.Key + " is miss")
}
